package com.prs.crm.action.project;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.project.ProjectSource;
import com.prs.crm.domain.project.ProjectType;
import com.prs.crm.service.project.ProjectSourceService;
import com.prs.crm.service.sys.LogService;

public class ProjectSourceAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ProjectSourceService projectSourceService;

	private PaginationSupport pagination;
    private String sourceName;
	private ProjectSource source;

	private Integer[] sourceIds;
	private LogService logService;

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public void setProjectSourceService(
			ProjectSourceService projectSourceService) {
		this.projectSourceService = projectSourceService;
	}

	public ProjectSourceService getProjectSourceService() {
		return projectSourceService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public ProjectSource getSource() {
		return source;
	}

	public void setSource(ProjectSource source) {
		this.source = source;
	}

	public void setSourceIds(Integer[] sourceIds) {
		this.sourceIds = sourceIds;
	}

	public Integer[] getSourceIds() {
		return sourceIds;
	}

	@Action("listSource")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProjectSource.class);
		this.setPagination(this.getProjectSourceService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editSource")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveSource", results = {
			@Result(name = "success", type = "redirect", location = "listSource"),
			@Result(name = "error", type = "chain", location = "editSource") })
	public String save() {
		if (source.getName() == null || "".equals(source.getName().trim())) {
			addActionError("来源不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(ProjectSource.class,
				source.getName())) {
			if(!(source.getName().equals(sourceName))){
			   this.addActionError("名称已经存在");
			   return ERROR;
			}
		}
		this.getProjectSourceService().save(source);
		return SUCCESS;
	}

	public String getSourceName() {
		return sourceName;
	}

	public void setSourceName(String sourceName) {
		this.sourceName = sourceName;
	}

	@Action(value = "removeSource", results = {
			@Result(name = "success", type = "redirect", location = "listSource"),
			@Result(name = "error", type = "chain", location = "listSource") })
	public String remove() {
		if (sourceIds == null) {
			this.addActionError("请选择要删除的类型");
			return ERROR;
		}
		for (Integer id : sourceIds) {
			ProjectSource source = this.getProjectSourceService().get(id);
			if (source.getProjects().size() != 0) {
				this.addActionError("已有任务引用，不能删除");
				return ERROR;
			}
		}
		this.getProjectSourceService().batchRemove(sourceIds);
		return SUCCESS;
	}

}
